﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMS2.Models;

namespace CMS2.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class ClassController : Controller
    {
        private CMSContext db = new CMSContext();

        //
        // GET: /Class/

        public ViewResult Index()
        {
            return View(db.Classes.ToList());
        }

        //
        // GET: /Class/Details/5

        public ViewResult Details(int id)
        {
            Class MyClass = db.Classes.Find(id);
            return View(MyClass);
        }

        //
        // GET: /Class/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Class/Create

        [HttpPost]
        public ActionResult Create(Class MyClass)
        {
            //TODO only unique classes shud be added in order of sections
            if (ModelState.IsValid)
            {
                MyClass.Section = MyClass.Section.ToUpper();
                db.Classes.Add(MyClass);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(MyClass);
        }
        
        //
        // GET: /Class/Edit/5
 
        public ActionResult Edit(int id)
        {
            Class MyClass = db.Classes.Find(id);

            List<Subject> subjects = db.Subjects.ToList();
            IEnumerable<SelectListItem> items = subjects.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() });

            ViewBag.sub = items;

            return View(MyClass);
        }

        //
        // POST: /Class/Edit/5

        [HttpPost]
        public ActionResult Edit(Class MyClass)
        {
            if (ModelState.IsValid)
            {
                db.Entry(MyClass).State = EntityState.Modified;               
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(MyClass);
        }
        
        //
        // GET: /Class/Delete/5
 
        public ActionResult Delete(int id)
        {
            Class MyClass = db.Classes.Find(id);
            return View(MyClass);
        }

        //
        // POST: /Class/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Class MyClass = db.Classes.Find(id);
            db.Classes.Remove(MyClass);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        //
        //Allocate Subject
        public ViewResult AllocateSubject(int id)
        {
            return View();
        }

        public ViewResult AllocateSubject()
        {
            return View();
        }


        public ViewResult MarkAttendance()
        {
            return View();
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}